package com.example.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.example.common.enums.ResultCodeEnum;
import com.example.common.enums.RoleEnum;
import com.example.entity.Account;
import com.example.entity.Business;
import com.example.entity.Rider;
import com.example.exception.CustomException;
import com.example.mapper.RiderMapper;
import com.example.service.RiderService;
import com.example.utils.TokenUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 骑手表业务处理实现方法
 **/
@Slf4j
@Service
public class RiderServiceImpl implements RiderService {

    @Resource
    private RiderMapper riderMapper;

    @Override
    public void add(Rider rider) {
        try{
            Rider rider1 = riderMapper.selectByUsername(rider.getUsername());
            if (ObjectUtil.isNotEmpty(rider1)) {
                throw new CustomException(ResultCodeEnum.USER_EXIST_ERROR);
            }
            if(ObjectUtil.isEmpty(rider.getName())){
                rider.setName(rider.getUsername());
            }

            // 设置角色
            rider.setRole(RoleEnum.RIDER.name());

            riderMapper.insert(rider);
        }catch (Exception e){
            log.error("添加骑手时发生错误: {}", rider, e);
            throw new RuntimeException("添加骑手失败", e);

        }
    }

    @Override
    public void register(Account account) {
        // 检查输入是否为 null，避免 NullPointerException
        if (account == null) {
            throw new IllegalArgumentException("骑手不能为空");
        }
        Rider rider = new Rider();
        BeanUtils.copyProperties(account, rider);
        this.add(rider);
    }

    @Override
    public Account login(Account account) {
        if (account == null) {
            throw new IllegalArgumentException("骑手不能为空");
        }
        Account rider = riderMapper.selectByUsername(account.getUsername());
        if (ObjectUtil.isNull(rider)) {
            throw new CustomException(ResultCodeEnum.USER_NOT_EXIST_ERROR);
        }
        if (!account.getPassword().equals(rider.getPassword())) {
            throw new CustomException(ResultCodeEnum.USER_ACCOUNT_ERROR);
        }
        // 检查审核状态
        if(RoleEnum.RIDER.name().equals(account.getRole())){
            Rider dbrider = selectById(rider.getId());
            if (!"通过".equals(dbrider.getStatus())) {
                throw new CustomException(ResultCodeEnum.NO_PASS);
            }
        }
        // 生成token
        String tokenData = rider.getId() + "-" + RoleEnum.RIDER.name();
        String token = TokenUtils.createToken(tokenData, rider.getPassword());
        rider.setToken(token);
        return rider;
    }

    @Override
    public void updatePassword(Account account) {
        Rider dbRider = riderMapper.selectByUsername(account.getUsername());
        if (!account.getPassword().equals(dbRider.getPassword())) {
            throw new CustomException(ResultCodeEnum.PARAM_PASSWORD_ERROR);
        }
        dbRider.setPassword(account.getNewPassword());
        riderMapper.updateById(dbRider);
    }

    @Override
    public void deleteById(Integer id) {
        riderMapper.deleteById(id);
    }

    @Override
    public void deleteBatch(List<Integer> ids) {
        for (Integer id : ids) {
            riderMapper.deleteById(id);
        }
    }

    @Override
    public void updateById(Rider rider) {
        Rider rider1 = riderMapper.selectByUsername(rider.getUsername());
        if (ObjectUtil.isNotEmpty(rider1) && !Objects.equals(rider1.getId(), rider.getId())) {
            throw new CustomException(ResultCodeEnum.USER_EXIST_ERROR);
        }
        riderMapper.updateById(rider);
    }

    @Override
    public Rider selectById(Integer id) {
        return riderMapper.selectById(id);
    }

    @Override
    public List<Rider> selectAll(Rider rider) {
        return riderMapper.selectAll(rider);
    }

    @Override
    public PageInfo<Rider> selectPage(Rider rider, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Rider> list = riderMapper.selectAll(rider);
        return PageInfo.of(list);
    }

}
